﻿namespace SimpleCalculator
{
    using System;

    using CalculatorLibrary;

    /// <summary>
    ///     Class Program
    /// </summary>
    public class Program
    {
        #region Public Methods and Operators

        /// <summary>
        ///     Defines the entry point of the application.
        /// </summary>
        public static void Main()
        {
            var calculator = new Calculator();
            Console.WriteLine(calculator.GetUsageHelp());
            Console.WriteLine("Specify expression, you want to calculate (or one of usage keys for help):");
            do
            {
                var inputString = Console.ReadLine();
                var result = calculator.Calculate(inputString);
                if (result.ReturnState != ReturnState.Continue)
                {
                    Environment.Exit(0);
                }

                if (result.IsError)
                {
                    Console.WriteLine("Error: {0}", result.StringResult);
                }
                else
                {
                    if (result.Result.HasValue)
                    {
                        Console.WriteLine("Result: {0}", result.Result.Value.ToString("G"));
                    }
                    else
                    {
                        Console.WriteLine(
                            !string.IsNullOrEmpty(result.StringResult) ? result.StringResult : "No results to display!");
                    }
                }

                Console.WriteLine("Specify new expression:");
            }
            while (true);
            // ReSharper disable once FunctionNeverReturns
        }

        #endregion
    }
}